import requests
# url = "https://cg-sycdn.kuwo.cn/80a3062ae7e7216e3b19bb476a53562a/61813562/resource/n1/25/27/278719973.mp3"
# with open("天外来物.mp3", mode="wb") as f:
#     f.write(requests.get(url).content)
#     print("下载成功")


#https://cg-sycdn.kuwo.cn
#http://www.kuwo.cn/api/v1/www/music/playUrl    mid
#http://www.kuwo.cn/api/www/artist/artistMusic  artistid
#http://www.kuwo.cn/api/www/search/searchMusicBykeyWord 歌手id

# searchMusicBykeyWord_url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord"
# header = {
#     "Accept": "application/json, text/plain, */*",
#     "Accept-Encoding": "gzip, deflate",
#     "Accept-Language": "zh-CN,zh;q=0.9",
#     "Connection": "keep-alive",
#     "Cookie": "_ga=GA1.2.114017100.1635857687; _gid=GA1.2.1967806105.1635857687;Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1635857754,1635859472,1635861062,1635861863;Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1635861863; _gat=1; kw_token=RK49TLEYIC",
#     "csrf": "RK49TLEYIC",
#     "Host": "www.kuwo.cn",
#     "Referer": "http://www.kuwo.cn/search/list?key=%E8%96%9B%E4%B9%8B%E8%B0%A6",
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"
# }
# params = {
#     "key": "周杰伦",
#     "pn": "1",
#     "rn": "30",
#     "httpsStatus": "1",
#     "reqId": "bab03630-3be6-11ec-aa72-75f7f561d77b"
# }
# resp = requests.get(searchMusicBykeyWord_url, headers=header, params=params)
# artistid1 = resp.json()["data"]["list"][0]["artistid"]
#
# artistid_params = {
#     "artistid": str(artistid1),
#     "pn": "2",
#     "rn": "30",
#     "httpsStatus": "1",
#     "reqId": "bab03630-3be6-11ec-aa72-75f7f561d77b"
# }
# mid_url = "http://www.kuwo.cn/api/www/artist/artistMusic"
# resp_mid = requests.get(mid_url, headers=header, params=artistid_params)
# datas = resp_mid.json()["data"]["list"]
# for i in datas:
#     name = i["name"]
#     artist = i["artist"]
#     rid = i["rid"]
#     real_url = "http://www.kuwo.cn/api/v1/www/music/playUrl"
#     real_params = {
#         "mid": str(rid),
#         "type": "music",
#         "httpsStatus": "1",
#         "reqId": "bab03630-3be6-11ec-aa72-75f7f561d77b"
#     }
#     resp_real = requests.get(real_url, params=real_params, headers=header)
#     song_url = resp_real.json()["data"]["url"]
#     with open(f"music/{name}.mp3", mode="wb") as f:
#         f.write(requests.get(song_url).content)
#         print(name+"下载成功")

#https://sv-sycdn.kuwo.cn/8287ab6fef9e2d1e511cfa08558fb0dc/6181d483/resource/n2/66/23/2672962327.mp3 真实下载地址
#http://www.kuwo.cn/api/v1/www/music/playUrl            找真实下载地址
#http://www.kuwo.cn/api/www/search/searchMusicBykeyWord 找rid

astist_name = input("输入歌手名：")
pn = input("输入第几页：")
save_path = input("输入要保存音乐的绝对路径(注意\要改为/)：")

rid_url ="http://www.kuwo.cn/api/www/search/searchMusicBykeyWord"
params = {
    "key": str(astist_name),
    "pn": str(pn),
    "rn": "30",
    "httpsStatus": "1",
    "reqId": "1533dc20-3c3b-11ec-9fa8-37644efea321"
}
header = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Cookie": "_ga=GA1.2.114017100.1635857687; _gid=GA1.2.1967806105.1635857687; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1635865295,1635865313,1635865334,1635897840; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1635897861; kw_token=GHTXHTZSQD9",
    "csrf": "GHTXHTZSQD9",
    "Host": "www.kuwo.cn",
    "Referer": "http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"
}
resp = requests.get(rid_url, headers=header, params=params)
datas = resp.json()["data"]["list"]
n=0
for i in datas:
    name = i["name"]
    rid = i["rid"]
    artist = i["artist"]
    print(f"{n} 第{pn}页有"+artist+"的"+name)
    n = n+1
flag2 = input("是否进行下载（是或否）：")
if flag2 == "是":
    for k in datas:
        name = k["name"]
        rid = k["rid"]
        artist = k["artist"]
        real_url = "http://www.kuwo.cn/api/v1/www/music/playUrl"
        real_params = {
            "mid": str(rid),
            "type": "music",
            "httpsStatus": "1",
            "reqId": "1533dc20-3c3b-11ec-9fa8-37644efea321"
        }
        real_resp = requests.get(real_url, params=real_params, headers=header)
        flag = real_resp.json()["msg"]
        if flag == "该歌曲为付费内容，请下载酷我音乐客户端后付费收听":
            print(artist+" 的 "+name+" 为 "+flag+"（爬不到）")
            continue
        else:
            song_url = real_resp.json()["data"]["url"]
            with open(f"{save_path}/{name}.mp3", mode="wb") as f:
                f.write(requests.get(song_url).content)
                print(name+"下载完毕")
else:
    print("没找到想下的歌曲吗？可以通过更改页号搜索该歌手的其他歌曲")